/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * Copyright (C) 2005-2008 Alfresco Software Limited.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

package node.manager.console;

import java.io.Console;
import java.util.Scanner;
import node.manager.NodeManager;
import node.manager.actions.NodeImpl;
import node.manager.alfresco.ServerInfo;

public class NodeManagerConsole {

    public NodeManagerConsole() {
    }

    public void main() {

        Scanner sc = new Scanner(System.in);
        Console console = System.console();

        System.out.println("\n--------------------------------");
        System.out.println("- ALFRESCO NODE MANAGER (v" + NodeManager.NM_VERSION + ") -");
        System.out.println("--------------------------------");

        if (console == null)
        {
            System.out.println("Unable to obtain console.");
            System.exit(0);
        }

        String host = console.readLine("\nAlfresco Host: ");
        String port = console.readLine("\nPort: ");
        String user = console.readLine("\nUser: ");
        String passwd = new String(console.readPassword("%s", "\nPassword: "));

        ServerInfo sv = new ServerInfo();
        sv.setHost(host);
        sv.setPort(port);
        sv.setUser(user);
        sv.setPass(String.valueOf(passwd));
        NodeImpl node = null;

        try {
            node = NodeImpl.getInstance(sv);

            if (node.getNode() == null && !node.getMsg().equals("")) {
                System.out.println(node.getMsg());
                throw new Exception();
            }

            System.out.println("\n----------------------------");
            System.out.println("Type \"exit\" to stop OR \"ctrl + c\" at anytime");
            System.out.println("----------------------------");

            String nodeRef;

            do {
                System.out.println("\nNode Reference to delete: ");
                nodeRef = sc.nextLine();

                if (!nodeRef.equals("exit")) {
                    node.setNode(nodeRef);
                    node.deleteNode();
                    System.out.println(node.getMsg());
                }
            } while (!nodeRef.equals("exit"));

        } catch (Exception ex) {
            if (node == null) {
                String msg = "\n";

                if (node != null && !node.getMsg().equals("")) {
                    msg += node.getMsg();
                } else {
                    msg += "Error starting session. Please verify that you have "
                            + "entered the server name and port number correctly.";
                }

                System.out.println(msg);
            }

        } finally {
            System.out.println("\n--------------------------------------------");
            System.out.println("Author: Danilo Buzar (danilobuzar@gmail.com)");
            System.out.println("--------------------------------------------");
        }
    }
}
